home *** CD-ROM | disk | FTP | other *** search
/ Libris Britannia 4 / science library(b).zip / science library(b) / UTILITIE / CONVERSI / 0825C.ZIP / DUMP.ASM < prev    next >
Assembly Source File  |  1980-01-01  |  12KB  |  340 lines

  1.         name    dump
  2.         page    55,132
  3.         title   'DUMP --- Display File Contents' 
  4.  
  5. ; DUMP --- a utility to display the contents of a file in hex
  6. ; and ASCII format.  Requires PC-DOS 2.0 or MS-DOS 2.0.
  7.  
  8. ; Used in the form:
  9. ; A>dump path\filename.ext  [ >device ]
  10. ; (item in square brackets is optional)
  11.  
  12. ; version 1.0   March 25, 1984 
  13. ; Copyright (c) 1984 by Ray Duncan
  14. ; May be freely reproduced for non-commercial use. 
  15.  
  16. cr      equ     0dh             ;ASCII carriage return
  17. lf      equ     0ah             ;ASCII line feed
  18. blank    equ    20h        ;ASCII space code
  19.  
  20. command equ     80h             ;buffer for command tail
  21.  
  22. blksize equ     128             ;size of input file records
  23.  
  24. output_handle equ 1             ;handle of standard output device
  25.                                 ;(can be redirected)
  26. error_handle equ 2        ;handle of standard error device
  27.                 ;(not redirectable)
  28.  
  29. cseg    segment para public 'CODE'
  30.  
  31.         assume  cs:cseg,ds:data,es:data,ss:stack
  32.  
  33.  
  34. dump    proc    far             ;entry point from PC-DOS
  35.  
  36.         push    ds              ;save DS:0000 for final
  37.         xor     ax,ax           ;return to PC-DOS
  38.         push    ax
  39.         mov     ax,data         ;make our data segment
  40.         mov     es,ax           ;addressable via ES register.
  41.         mov     ah,30h        ;check version of PC-DOS.    
  42.         int     21h
  43.         cmp     al,2
  44.         jae     dump1           ;proceed, DOS 2.0 or greater.
  45.         mov     dx,offset msg3  ;DOS 1.x --- print error message;
  46.     mov    ax,es        ;we must use the old PC-DOS
  47.     mov    ds,ax        ;string output function since
  48.     mov    ah,9        ;handles are not available in
  49.     int    21h        ;this version of PC-DOS.
  50.     ret
  51.  
  52. dump1:  call    get_filename    ;get path and file spec. for
  53.                                 ;input file from command line tail.
  54.         mov     ax,es           ;set DS=ES for remainder
  55.         mov     ds,ax           ;of program.
  56.         jnc     dump2           ;jump, got acceptable name.
  57.         mov     dx,offset msg2  ;missing or illegal filespec,
  58.     mov    cx,msg2_length
  59.         jmp     dump9           ;print error message and exit.
  60.  
  61. dump2:  call    open_input      ;now try to open input file
  62.         jnc     dump3           ;jump,opened input ok
  63.         mov     dx,offset msg1  ;open of input file failed,
  64.     mov    cx,msg1_length
  65.         jmp     dump9           ;print error msg and exit.
  66.  
  67. dump3:  call    read_block      ;initialize input file buffer
  68.      jnc    dump4        ;jump,got a block
  69.     mov    dx,offset msg4    ;empty file,print error
  70.     mov    cx,msg4_length
  71.     jmp    dump9        ;message and exit
  72.  
  73.                                 ;file successfully opened,             
  74. dump4:                          ;now convert and display it! 
  75.         call    get_char        ;read 1 character from input.
  76.         jc      dump8           ;jump, end of file
  77.     inc    input_addr    ;update relative file position
  78.     or    bx,bx        ;is this 1st char of block?
  79.     jnz    dump5        ;no
  80.     call    print_heading
  81. dump5:  and     bx,0fh        ;is this first byte of 16?
  82.     jnz    dump6        ;no,jump
  83.     push    ax        ;save the byte
  84.     mov    di,offset output;convert relative file addr. 
  85.     mov    ax,input_addr    ;for output string
  86.     call    conv_word 
  87.     pop    ax
  88. dump6:                ;store ASCII version of character,
  89.                 ;if it is alphanumeric,
  90.     mov    di,offset outputb
  91.     add    di,bx        ;calculate output string address
  92.     mov    byte ptr [di],'.' ;if it is control character,
  93.     cmp    al,blank    ;just print a dot.    
  94.     jb    dump7         ;jump, not alphanumeric.
  95.     cmp    al,7eh        
  96.     ja    dump7         ;jump, not alphanumeric.
  97.     mov    [di],al        ;store ASCII character.
  98. dump7:                ;now convert binary byte
  99.                 ;to hex  ASCII equivalent.
  100.     push    bx        ;save offset 0-15 of this byte.
  101.                 ;calc. its position in
  102.                 ;output string.
  103.     mov    di,offset outputa
  104.     add    di,bx        ;base addr + (offset*3)
  105.     add    di,bx
  106.     add    di,bx
  107.     call    conv_byte    ;convert data byte to hex 
  108.     pop    bx        ;restore byte offset
  109.     cmp    bx,0fh        ;16 bytes converted yet?
  110.     jne    dump4        ;no,get another byte
  111.     mov    dx,offset output
  112.     mov    cx,output_length
  113.     call    write_std    ;yes, print the line
  114.         jmp     dump4           ;get next char. from input file.
  115.  
  116. dump8:                          ;end of file detected,
  117.         call    close_input     ;close input file. 
  118.         ret                     ;now return to PC-DOS.
  119.  
  120. dump9:                          ;come here to print message 
  121.                 ;on standard error device, 
  122.         call    write_error     ;and return control to PC-DOS
  123.         ret
  124.  
  125. dump    endp
  126.  
  127.  
  128. get_filename proc near          ;process name of input file
  129.                                 ;return Carry = 0 if successful
  130.                 ;return Carry = 1 if no filename
  131.                 ;DS:SI <- addr command line     
  132.         mov     si,offset command
  133.                                 ;ES:DI <- addr filespec buffer
  134.         mov     di,offset input_name
  135.         cld
  136.         lodsb                   ;any command line present?
  137.         or      al,al           ;return error status if not.
  138.         jz      get_filename4
  139. get_filename1:                  ;scan over leading blanks
  140.         lodsb                   ;to file name.
  141.         cmp     al,cr           ;if we hit carriage return...
  142.         je      get_filename4   ;jump, name is missing
  143.         cmp     al,20h          ;is this a blank?
  144.         jz      get_filename1   ;if so keep scanning.
  145. get_filename2:                  ;found first char of name,
  146.         stosb                   ;move last char. to output
  147.                                 ;file name buffer. 
  148.         lodsb                   ;check next character, found
  149.         cmp     al,cr           ;carriage return yet?   
  150.         je      get_filename3   ;yes,exit with success code.
  151.         cmp     al,20h          ;is this a blank?
  152.         jne     get_filename2   ;if not keep moving chars.
  153. get_filename3:                  ;exit with carry =0
  154.         clc                     ;for success flag
  155.         ret
  156. get_filename4:                  ;exit with carry =1
  157.         stc                     ;for error flag
  158.         ret
  159. get_filename endp 
  160.  
  161. open_input proc near            ;open input file
  162.                                 ;DS:DX=addr filename
  163.         mov     dx,offset input_name
  164.         mov     al,0            ;AL=0 for read only
  165.         mov     ah,3dh          ;function 3dh=open
  166.         int     21h             ;handle returned in AX,
  167.         mov     input_handle,ax ;save it for later.
  168.         ret                     ;CY is set if error
  169. open_input endp
  170.  
  171. close_input proc near           ;close input file
  172.         mov     bx,input_handle ;BX=handle
  173.         mov     ah,3eh
  174.         int     21h
  175.         ret
  176. close_input endp
  177.  
  178. get_char proc   near            ;get one character from input buffer
  179.                 ;return AL = char, BX = buffer offset
  180.                 ;return CY flag = 1 if end of file
  181.         mov     bx,input_ptr    ;is pointer at end of buffer?
  182.         cmp     bx,blksize      
  183.         jne     get_char1       ;no,jump
  184.                                 ;yes, buffer is exhausted, 
  185.     mov    input_ptr,0
  186.         call    read_block      ;new block must be read from disk.
  187.     jnc    get_char    ;got block, start routine over.
  188.     ret            ;end of file detected
  189.                 ;so return CY flag = True.
  190. get_char1:            ;get data byte into AL,
  191.         mov     al,[input_buffer+bx]
  192.         inc     input_ptr    ;bump input buffer pointer.
  193.     clc            ;return CY flag =0 since 
  194.         ret            ;not end of file.
  195. get_char endp   
  196.  
  197.  
  198. read_block proc near            ;read block of data from input file.
  199.                 ;return CY flag = 0 if read ok.
  200.                 ;       CY flag = 1 if end of file.
  201.         mov     bx,input_handle ;request read from PC-DOS.
  202.         mov     cx,blksize
  203.         mov     dx,offset input_buffer
  204.         mov     ah,3fh
  205.         int     21h
  206.                 ;initialize pointers
  207.     inc    input_block
  208.         mov     input_ptr,0 
  209.     or    ax,ax        ;was anything read in? (the OR 
  210.                 ; incidentally turns off the CY flag)
  211.     jnz    read_block1    ;yes,jump
  212.     stc            ;no,end of file so return CY=True
  213. read_block1:
  214.         ret
  215. read_block endp
  216.  
  217. write_std proc     near        ;write string to standard output.
  218.                 ;call DX = addr of output string
  219.                 ;     CX = length of string
  220.         mov     bx,output_handle;BX=handle for standard list device.
  221.         mov     ah,40h          ;function 40h=write to device.
  222.         int     21h             ;request service from DOS.
  223.         ret
  224. write_std endp
  225.  
  226. write_error proc near        ;write string to standard error device.
  227.                 ;call DX = addr of output string
  228.                 ;     CX = length of string
  229.         mov     bx,error_handle ;BX=handle for standard error device.
  230.         mov     ah,40h          ;function 40h=write to device.
  231.         int     21h             ;request service from DOS.
  232.         ret
  233. write_error endp
  234.  
  235. print_heading proc near         ;print record number and heading
  236.      push    ax        ;for a block of data
  237.      push    bx        ;first save registers     
  238.      mov    di,offset headinga
  239.      mov    ax,input_block 
  240.      call    conv_word    ;convert record number to ASCII
  241.     mov    dx,offset heading
  242.     mov    cx,heading_length
  243.     call    write_std    ;now print heading
  244.      pop    bx        ;restore registers
  245.      pop    ax
  246.         ret            ;and exit
  247. print_heading endp
  248.  
  249. conv_word proc near        ;convert 16-bit binary word 
  250.                 ; to hex ASCII
  251.                 ;call with AX=binary value
  252.                 ;          DI=addr to store string
  253.                 ;returns AX, DI, CX destroyed
  254.     push    ax
  255.     mov    al,ah
  256.     call    conv_byte    ;convert upper byte    
  257.     pop    ax
  258.     call    conv_byte    ;convert lower byte
  259.     ret
  260. conv_word endp
  261.  
  262. conv_byte proc    near          ;convert binary byte to hex ASCII
  263.                                 ;call with AL=binary value
  264.                                 ;          DI=addr to store string
  265.                                 ;returns   AX, DI, CX modified 
  266.  
  267.     sub    ah,ah        ;clear upper byte
  268.     mov    cl,16
  269.     div    cl        ;divide binary data by 16
  270.     call    ascii        ;the quotient becomes the first
  271.     stosb            ;ASCII character
  272.     mov    al,ah
  273.     call    ascii        ;the remainder becomes the
  274.     stosb            ;second ASCII character
  275.     ret
  276. conv_byte endp
  277.  
  278. ascii   proc     near        ;convert bottom 4 bits in AL 
  279.         add     al,'0'        ;into the hex ASCII character
  280.         cmp     al,'9'
  281.         jle     ascii2        ;jump if in range 0-9,
  282.         add     al,'A'-'9'-1    ;offset it to range A-F,
  283. ascii2:    ret            ;return ASCII char. in AL.
  284. ascii    endp
  285.  
  286. cseg    ends
  287.  
  288.  
  289. data    segment para public 'DATA'
  290.  
  291. input_name      db      64 dup (0)      ;buffer for input filespec
  292.  
  293. input_handle    dw      0               ;token from PCDOS for input file.
  294.  
  295. input_ptr       dw      0               ;pointer to input deblocking buffer
  296.  
  297. input_addr      dw      -1              ;relative address in file 
  298. input_block     dw      0               ;current 128 byte block number
  299.  
  300. output        db    'nnnn',blank,blank
  301. outputa        db    16 dup ('00',blank)
  302.         db    blank
  303. outputb         db    '0123456789ABCDEF',cr,lf 
  304. output_length    equ    $-output
  305.  
  306. heading        db    cr,lf,'Record',blank
  307. headinga    db    'nnnn',blank,blank,cr,lf
  308.         db    7 dup (blank)
  309.         db    '0  1  2  3  4  5  6  7  '
  310.         db    '8  9  A  B  C  D  E  F',cr,lf
  311. heading_length  equ    $-heading
  312.  
  313. input_buffer    db      blksize dup (?) ;deblocking buffer for input file
  314.  
  315. msg1            db      cr,lf
  316.                 db      'Cannot find input file.'
  317.                 db      cr,lf
  318. msg1_length    equ    $-msg1
  319.  
  320. msg2            db      cr,lf
  321.                 db      'Missing file name.'
  322.                 db      cr,lf
  323. msg2_length    equ    $-msg2
  324.  
  325. msg3            db      cr,lf
  326.                 db      'Requires PC-DOS version 2 or greater.'
  327.                 db      cr,lf,'$'
  328.  
  329. msg4        db    cr,lf,'Empty file.',cr,lf
  330. msg4_length    equ    $-msg4
  331.  
  332. data    ends    
  333.  
  334.  
  335. stack   segment para stack 'STACK'
  336.         db      64 dup (?)
  337. stack   ends
  338.  
  339.         end     dump
  340.